package com.android.canvasproject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.database.SQLException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;

import com.android.canvasproject.model.dao.ProviderDaoOpenHelper;
import com.android.canvasproject.model.entity.Canvas;
import com.android.canvasproject.model.entity.Postit;
import com.android.canvasproject.view.utils.ComponentViewUtil;
import com.android.canvasproject.view.utils.ConstantsAction;
import com.android.canvasproject.view.utils.ProjectModelCanvasUtil;

public class CanvasProjectActivity extends Activity {

	private Canvas c;
	public static RelativeLayout relative;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_canvas_project);
		setupActionBar();

		c = (Canvas) getIntent().getSerializableExtra(ConstantsAction.IntentsKey.CANVAS_ACTIVITY_INTENT);
		relative = (RelativeLayout) findViewById(R.id.project_model_canvas);
		relative.setTag(c.getId());

		ProjectModelCanvasUtil.drawQuadrants(this);

		PostitAsyncTask postitAsyncTask = new PostitAsyncTask(this);
		postitAsyncTask.execute(c.getId(), relative);

//		registerForContextMenu(relative);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		getActionBar().hide();
	}

	private void setupActionBar() {
		getActionBar().setDisplayHomeAsUpEnabled(true);
	}
	
	@Override
	public void onBackPressed() {
		NavUtils.navigateUpFromSameTask(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_activity_actions, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.findItem(R.id.action_search).setVisible(false);
		menu.findItem(R.id.action_new).setVisible(false);

		return super.onPrepareOptionsMenu(menu);
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);

		menu.setHeaderTitle("Ações");
		menu.add(0, v.getId(), 0, "Novo Postit");
		menu.add(0, v.getId(), 0, "Detalhes");
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	class PostitAsyncTask extends AsyncTask<Object, Void, List<Postit>> {

		private ProgressDialog asyncDialog;
		public PostitAsyncTask(Activity activity) {
			asyncDialog = new ProgressDialog(activity);
		}

		@Override
		protected void onPreExecute() {
			asyncDialog.setMessage("Loading...");
			asyncDialog.show();

			super.onPreExecute();
		}
		
		@Override
		protected List<Postit> doInBackground(Object... params) {
			ProviderDaoOpenHelper provider = new ProviderDaoOpenHelper(CanvasProjectActivity.this);
			List<Postit> postits = new ArrayList<Postit>();

			try {
				List<Map<String, Object>> result = provider.findFilter(Postit.PostitDao.TB_POSTIT, Postit.PostitDao.COLS_POSTIT, "id_canvas = " + params[0]);
				for(Map<String, Object> map : result){
					Postit p = Postit.entityStructure(map);
					postits.add(p);
				}

			} catch (SQLException e) {
				Log.e(Postit.PostitDao.TB_POSTIT, ConstantsAction.Database.QUERY, e);

			} finally {
				provider.cleanup();
			}
			return postits;
		}
		
		@Override
		protected void onPostExecute(List<Postit> result) {
			for(Postit p : result){
				View v = ComponentViewUtil.createNewPostit(CanvasProjectActivity.this, p.getPositionX(), p.getPositionY(), p);
				relative.addView(v);
			}
			asyncDialog.dismiss();
		}
	}
}